package day_2022_12_to_3.day;

import java.util.LinkedList;
import java.util.List;

/**
 * @author haomin
 * @date 2022/12/10 22:07
 **/
public class Day12_10_Review {
    class Solution {
        StringBuffer sb = new StringBuffer();
        public String longestPalindrome(String s) {
            for(int i = 0; i < s.length(); ++i) {
                isReverse(s, i, i);
                isReverse(s, i, i+1);
            }
            return sb.toString();
        }
        public void isReverse(String s, int l, int r) {
            while(l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
                l--; r++;
            }
            if(r-l-1 > sb.length()) sb = new StringBuffer(s.substring(l+1, r));
        }
    }

    class Solution2 {
        public String convert(String s, int numRows) {
            if(numRows < 2) return s;
            List<StringBuffer> list = new LinkedList<>();
            for(int i = 0; i < numRows; ++i) list.add(new StringBuffer());
            int flag = -1, row = 0;
            for(int i = 0; i < s.length(); ++i) {
                list.get(row).append(s.charAt(i));
                if(row == 0 || row == numRows-1) flag = -flag;
                row += flag;
            }
            StringBuffer res = new StringBuffer();
            for(int i = 0; i < list.size(); ++i) {
                res.append(list.get(i));
            }
            return res.toString();
        }
    }
}